package com.wie.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wie.domain.entity.SensorData;
import com.wie.domain.vo.SensorDataVO;
import com.wie.properties.BemfaProperties;
import com.wie.service.SensorDataService;
import com.wie.mapper.SensorDataMapper;
import com.wie.utils.HttpClientUtil;
import com.wie.utils.OkHttpUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;

/**
* @author WiedersehenM
* @description 针对表【sensor_data(传感器数据)】的数据库操作Service实现
* @createDate 2025-02-28 11:59:01
*/
@Service
public class SensorDataServiceImpl extends ServiceImpl<SensorDataMapper, SensorData>
    implements SensorDataService{

    @Autowired
    private BemfaProperties bemfaProperties;
    @Autowired
    private SensorDataMapper sensorDataMapper;


    /**
     * 获取传感器信息
     * 湿度、二氧化碳含量、有害气体检测、温度
     * @return
     */
    @Override
    public SensorDataVO fetchSensorData() throws IOException {
        // 准备请求数据
        String url = "https://apis.bemfa.com/va/getmsg";
        String uid = bemfaProperties.getUid();
        String type = "1";
        String topic1 = "HUMID";
        String topic2 = "SGPECO2";
        String topic3 = "SGPTVOC";
        String topic4 = "TEMP";
        // 获取湿度的请求
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("uid", uid);
        map1.put("topic", topic1);
        map1.put("type", type);
        String result1 = HttpClientUtil.doGet(url, map1);
        JSONObject jsonResult1 = JSON.parseObject(result1);
        String humid = jsonResult1.getString("msg");
        // 获取二氧化碳浓度的请求
        HashMap<String, String> map2 = new HashMap<>();
        map2.put("uid", uid);
        map2.put("topic", topic2);
        map2.put("type", type);
        String result2 = HttpClientUtil.doGet(url, map2);
        JSONObject jsonResult2 = JSON.parseObject(result2);
        String co2 = String.format("%.2f", jsonResult2.getDouble("msg"));
        // 获取有害气体检测的请求
        HashMap<String, String> map3 = new HashMap<>();
        map3.put("uid", uid);
        map3.put("topic", topic3);
        map3.put("type", type);
        String result3 = HttpClientUtil.doGet(url, map3);
        JSONObject jsonResult3 = JSON.parseObject(result3);
        String harmfulGas = String.format("%.2f", jsonResult3.getDouble("msg") * 1000);
        // 获取当前温度的请求
        HashMap<String, String> map4 = new HashMap<>();
        map4.put("uid", uid);
        map4.put("topic", topic4);
        map4.put("type", type);
        String result4 = HttpClientUtil.doGet(url, map4);
        JSONObject jsonResult4 = JSON.parseObject(result4);
        String createTime = jsonResult4.getString("time");
        Double temperatureF = jsonResult4.getDouble("msg");
        String temperature = String.format("%.2f", ((temperatureF - 32) * 5 / 9.00));
        // 封装 SensorDataVO
        SensorDataVO sensorDataVO = new SensorDataVO().builder()
                .humid(humid)
                .co2(co2)
                .harmfulGas(harmfulGas)
                .createTime(LocalDateTime.parse(createTime))
                .build();
        // 调用 mapper，保存至数据库
        SensorData sensorData = new SensorData();
        BeanUtils.copyProperties(sensorDataVO, sensorData);
        sensorDataMapper.insert(sensorData);
        return sensorDataVO;
    }
}




